package com.oot2d.world;

import com.oot2d.entity.Player;
import org.newdawn.slick.Graphics;
import com.oot2d.util.WorldManager;

public class Camera {
    private float x, y;

    private static final float SCREEN_WIDTH = 480.0f;
    private static final float SCREEN_HEIGHT = 320.0f;

    public Camera()
    {
        x = 0;
        y = 0;
    }

    public Camera(float cx, float cy)
    {
        x = cx;
        y = cy;
    }

    public float getX()
    {
        return x;
    }

    public float getY()
    {
        return y;
    }

    public void setX(float cx)
    {
        x = cx;
    }

    public void setY(float cy)
    {
        y = cy;
    }

    public boolean leftEdge()
    {
        return (x == 0.0f);
    }

    public boolean rightEdge()
    {
        return (x == (WorldManager.currentWorld.getWidth() - SCREEN_WIDTH));
    }

    public boolean topEdge()
    {
        return (y == 0.0f);
    }

    public boolean bottomEdge()
    {
        return (y == (WorldManager.currentWorld.getHeight() - SCREEN_HEIGHT));
    }

    public void lookAt(Player p)
    {
        x = p.getX() - (SCREEN_WIDTH / 2);
        y = p.getY() - (SCREEN_HEIGHT / 2);

        if (x < 0)
            x = 0.0f;
        else if ((x + SCREEN_WIDTH) >= WorldManager.currentWorld.getWidth())
            x = WorldManager.currentWorld.getWidth() - SCREEN_WIDTH;

        if (y < 0)
            y = 0.0f;
        else if ((y + SCREEN_HEIGHT) >= WorldManager.currentWorld.getHeight())
            y = WorldManager.currentWorld.getHeight() - SCREEN_HEIGHT;
    }

    public void lookAt(float lx, float ly)
    {
        x = lx - (SCREEN_WIDTH / 2);
        y = ly - (SCREEN_HEIGHT / 2);
    }

    //Check if we're inside the map bounds
    public void adjust(Graphics g)
    {
        g.translate(-Math.round(x), -Math.round(y));
    }

    //Shift the graphics context back
    public void reset(Graphics g)
    {
        g.translate(Math.round(x), Math.round(y));
    }
}
